using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
namespace SHDocVw
{
	internal sealed class DShellWindowsEvents_EventProvider : DShellWindowsEvents_Event, IDisposable
	{
		private IConnectionPointContainer m_ConnectionPointContainer;
		private ArrayList m_aEventSinkHelpers;
		private IConnectionPoint m_ConnectionPoint;
		private void Init()
		{
			IConnectionPoint connectionPoint = null;
			Guid guid = new Guid(new byte[]
			{
				224,
				6,
				65,
				254,
				154,
				57,
				208,
				17,
				164,
				140,
				0,
				160,
				201,
				10,
				143,
				57
			});
			this.m_ConnectionPointContainer.FindConnectionPoint(ref guid, out connectionPoint);
			this.m_ConnectionPoint = (IConnectionPoint)connectionPoint;
			this.m_aEventSinkHelpers = new ArrayList();
		}
		public override void add_WindowRegistered(DShellWindowsEvents_WindowRegisteredEventHandler windowRegisteredDelegate)
		{
			Monitor.Enter(this);
			try
			{
				if (this.m_ConnectionPoint == null)
				{
					this.Init();
				}
				DShellWindowsEvents_SinkHelper dShellWindowsEvents_SinkHelper = new DShellWindowsEvents_SinkHelper();
				int dwCookie = 0;
				this.m_ConnectionPoint.Advise((object)dShellWindowsEvents_SinkHelper, out dwCookie);
				dShellWindowsEvents_SinkHelper.m_dwCookie = dwCookie;
				dShellWindowsEvents_SinkHelper.m_WindowRegisteredDelegate = windowRegisteredDelegate;
				this.m_aEventSinkHelpers.Add((object)dShellWindowsEvents_SinkHelper);
			}
			finally
			{
				Monitor.Exit(this);
			}
		}
		public override void remove_WindowRegistered(DShellWindowsEvents_WindowRegisteredEventHandler dShellWindowsEvents_WindowRegisteredEventHandler)
		{
			Monitor.Enter(this);
			try
			{
				if (this.m_aEventSinkHelpers != null)
				{
					int count = this.m_aEventSinkHelpers.Count;
					int num = 0;
					if (0 < count)
					{
						DShellWindowsEvents_SinkHelper dShellWindowsEvents_SinkHelper;
						while (true)
						{
							dShellWindowsEvents_SinkHelper = (DShellWindowsEvents_SinkHelper)this.m_aEventSinkHelpers[num];
							if (dShellWindowsEvents_SinkHelper.m_WindowRegisteredDelegate != null && ((dShellWindowsEvents_SinkHelper.m_WindowRegisteredDelegate.Equals((object)dShellWindowsEvents_WindowRegisteredEventHandler) ? 1 : 0) & 255) != 0)
							{
								break;
							}
							num++;
							if (num >= count)
							{
								goto IL_E1;
							}
						}
						this.m_aEventSinkHelpers.RemoveAt(num);
						this.m_ConnectionPoint.Unadvise(dShellWindowsEvents_SinkHelper.m_dwCookie);
						if (count <= 1)
						{
							Marshal.ReleaseComObject(this.m_ConnectionPoint);
							this.m_ConnectionPoint = null;
							this.m_aEventSinkHelpers = null;
						}
					}
				}
				IL_E1:;
			}
			finally
			{
				Monitor.Exit(this);
			}
		}
		public override void add_WindowRevoked(DShellWindowsEvents_WindowRevokedEventHandler windowRevokedDelegate)
		{
			Monitor.Enter(this);
			try
			{
				if (this.m_ConnectionPoint == null)
				{
					this.Init();
				}
				DShellWindowsEvents_SinkHelper dShellWindowsEvents_SinkHelper = new DShellWindowsEvents_SinkHelper();
				int dwCookie = 0;
				this.m_ConnectionPoint.Advise((object)dShellWindowsEvents_SinkHelper, out dwCookie);
				dShellWindowsEvents_SinkHelper.m_dwCookie = dwCookie;
				dShellWindowsEvents_SinkHelper.m_WindowRevokedDelegate = windowRevokedDelegate;
				this.m_aEventSinkHelpers.Add((object)dShellWindowsEvents_SinkHelper);
			}
			finally
			{
				Monitor.Exit(this);
			}
		}
		public override void remove_WindowRevoked(DShellWindowsEvents_WindowRevokedEventHandler dShellWindowsEvents_WindowRevokedEventHandler)
		{
			Monitor.Enter(this);
			try
			{
				if (this.m_aEventSinkHelpers != null)
				{
					int count = this.m_aEventSinkHelpers.Count;
					int num = 0;
					if (0 < count)
					{
						DShellWindowsEvents_SinkHelper dShellWindowsEvents_SinkHelper;
						while (true)
						{
							dShellWindowsEvents_SinkHelper = (DShellWindowsEvents_SinkHelper)this.m_aEventSinkHelpers[num];
							if (dShellWindowsEvents_SinkHelper.m_WindowRevokedDelegate != null && ((dShellWindowsEvents_SinkHelper.m_WindowRevokedDelegate.Equals((object)dShellWindowsEvents_WindowRevokedEventHandler) ? 1 : 0) & 255) != 0)
							{
								break;
							}
							num++;
							if (num >= count)
							{
								goto IL_E1;
							}
						}
						this.m_aEventSinkHelpers.RemoveAt(num);
						this.m_ConnectionPoint.Unadvise(dShellWindowsEvents_SinkHelper.m_dwCookie);
						if (count <= 1)
						{
							Marshal.ReleaseComObject(this.m_ConnectionPoint);
							this.m_ConnectionPoint = null;
							this.m_aEventSinkHelpers = null;
						}
					}
				}
				IL_E1:;
			}
			finally
			{
				Monitor.Exit(this);
			}
		}
		public DShellWindowsEvents_EventProvider(object obj)
		{
			this.m_ConnectionPointContainer = (IConnectionPointContainer)obj;
		}
		public override void Finalize()
		{
			Monitor.Enter(this);
			try
			{
				if (this.m_ConnectionPoint != null)
				{
					int count = this.m_aEventSinkHelpers.Count;
					int num = 0;
					if (0 < count)
					{
						do
						{
							DShellWindowsEvents_SinkHelper dShellWindowsEvents_SinkHelper = (DShellWindowsEvents_SinkHelper)this.m_aEventSinkHelpers[num];
							this.m_ConnectionPoint.Unadvise(dShellWindowsEvents_SinkHelper.m_dwCookie);
							num++;
						}
						while (num < count);
					}
					Marshal.ReleaseComObject(this.m_ConnectionPoint);
				}
			}
			catch (Exception)
			{
			}
			finally
			{
				Monitor.Exit(this);
			}
		}
		public override void Dispose()
		{
			this.Finalize();
			GC.SuppressFinalize(this);
		}
	}
}
